<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>SSE 示例</title>
</head>
<body>
  <h1>实时消息</h1>
  <div id="messages"></div>

  <script>
    // 创建 SSE 连接
    const eventSource = new EventSource('http://127.0.0.1:8007/sse');

    // 监听服务器推送的事件
    eventSource.onmessage = function(event) {
      // 添加新消息到页面
      const messages = document.getElementById('messages');
      messages.innerHTML += event.data + '<br>';
    };

    // 处理连接错误
    eventSource.onerror = function(event) {
      console.error('SSE 连接出错:', event);
    };
  </script>
</body>
</html>
